const mongoose = require('mongoose')
const Message = require('../../model/message')
const Normal = require('../../model/normal')

module.exports = async (req, res) => {
    // console.log(req.fields);
    const {email,username,content} = req.fields
    try {
        var count = await Normal.find({email}).count();
        if(count==0){
            var {_id} = await Normal.create({email,username})
        }else{
            var {_id} = await Normal.findOne({email})
            var data1 = await Normal.updateOne({email},{username})
        }
         var data = await Message.create({author:_id,content})
        
    } catch (error) {
        console.log(error);
        return res.send({
            data: null,
            meta: {
                status: 401,
                msg: '添加失败'
            }
        })
    }
    res.send({
        data: null,
        meta: {
            status: 201,
            msg: '添加成功'
        }
    })
}